import httpx
from syncora_sdk.schemas.synthetic import SyntheticDataRequest
from syncora_sdk.schemas.dto.EncryptedApiKeyDto import EncryptedApiKeyDto
from syncora_sdk.utils.http.raise_for_status_with_details import (
    raise_for_status_with_details,
)


class SyncoraClient:
    def __init__(self, api_key: str) -> None:

        self.api_key = self.generateTheHash(api_key)
        self.client = httpx.Client(
            base_url="https://api.syncora.ai/api/v1",
            headers={"x-api-key": self.api_key},
            timeout=None,
        )

    def generateTheHash(self, x_api_key: str) -> str:
        response = httpx.post(
            "https://api.syncora.ai/api/v1/credentials/api/encrypt",
            json={"apiKey": x_api_key},
        )

        raise_for_status_with_details(response)

        data = response.json()

        enc_key = EncryptedApiKeyDto(data).data
        if not enc_key:
            raise ValueError(
                "Encryption failed: 'encryptedApiKey' missing in response."
            )

        return enc_key

    def post(self, path: str, json):
        response = self.client.post(path, json=json)
        raise_for_status_with_details(response)
        return response.json()

    def close(self):
        self.client.close()
